package com.ftg.learn.chapter05.dao;

import com.ftg.learn.chapter05.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapperResultSetExtractor;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

/**
 * 数据模型类,用jdbc完成对数据库的操作
 * @author kangjx
 * @date 2022/11/8
 */
@Repository
public class StudentDao {

    JdbcTemplate jdbcTemplate;

    @Autowired
    public void abc(DataSource dataSource){
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public List<Map<String,Object>> showAll(){
        return this.jdbcTemplate.queryForList("select * from student");
    }

    public List<Student> showAll1(){
//        public <T> T query(final String sql, final ResultSetExtractor<T> rse)
//        public <T> List<T> query(String sql, RowMapper<T> rowMapper)
       return this.jdbcTemplate.query("select sno,sname,sage,ssex from student",(rs,index)->{
            Student s = new Student();
            s.setSno(rs.getString(1));
            s.setSname(rs.getString(2));
            s.setSage(rs.getInt(3));
            s.setSsex(rs.getString(4));
            return s;
        });
    }
//
//    update()用来做添加、修改、删除
//    execute() ddl 创表  创库

}
